<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Org\Upload;
use App\Org\Image;
use DB;

class DisplayController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //主页显示
    public function index()
    {   
        //获取表中数据
        $data=DB::table("display")->orderBy('id','asc')->paginate(4);
                                   
        return view("admin.display.index",['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       return view("admin.display.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    //获取表单提交的信息
    public function store(Request $request)
    {   
        //表单验证
        $this->validate($request, [
        
        'photo' => 'required',
        "content"=>'required',
        ],[

        "photo.required"=>"上传图片不能为空",
        "content.required"=>"必要的文字说明不能为空！",

        ]);

        //获取表单提交信息
        $data=$request->all();
        unset($data['_token']);
        
        //-----执行文件的上传和比例缩放---------
        //实例化对象
        $upfile = new Upload("photo");
        $img = new Image();

        //执行文件上传
        $res = $upfile->doUpload();//没有比例缩放前文件的上传
        $res1=$upfile->path.$upfile->saveName;//获取没有缩放的文件地址
        $path=$upfile->path.$upfile->saveName;
        $saveFile=$upfile->path.'s_'.$upfile->saveName;

        //执行比例缩放文件的上传
        $img->open($path)->thumb(150,150)->save($saveFile);

        //获取上传文件的文件名称
        
        $photo=$upfile->saveName;
        $data['photo']=$photo;
        
        //将数据填入表格中
       $m= DB::table("display")->insert($data);

        if($m>0){
            return redirect("admin/display")->with('err','数据添加成功！');
        }else{
            return back()->with("err","添加失败！");
        }

    }

   
   

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {   
        $list=DB::table("display")->where("id",$id)->first();
        //dd($list);
        return view("admin.display.edit",['list'=>$list]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         //表单验证
        $this->validate($request, [
        
        'photo'=>"required",
        "content"=>'required',
        ],[

        "photo.required"=>"请选择变更图片！",
        "content.required"=>"必要的文字说明不能为空！",

        ]);

        //获取表单提交数据
        $data=$request->all();
        unset($data['_method']);
        unset($data['_token']);

        //-----执行文件的上传和比例缩放---------
        //实例化对象
        $upfile = new Upload("photo");
        $img = new Image();

        //执行文件上传
        $res = $upfile->doUpload();//没有比例缩放前文件的上传
        $res1=$upfile->path.$upfile->saveName;//获取没有缩放的文件地址
        $path=$upfile->path.$upfile->saveName;
        $saveFile=$upfile->path.'s_'.$upfile->saveName;

        //执行比例缩放文件的上传
        $img->open($path)->thumb(150,150)->save($saveFile);

        //获取上传文件的文件名称        
        $photo=$upfile->saveName;
        $data['photo']=$photo;     
        $m=DB::table("display")->where("id",$id)->update($data);
        //dd($m);
        if($m>0){
            return redirect('admin/display');
        }else{
           return back();
        }        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //删除指定信息
    public function destroy($id)
    {
        DB::table('display')->where('id',$id)->delete();
        return redirect("admin/display")->with('err',"删除成功！");
        
    }
}
